﻿using System;
using System.Collections.Generic;
using BLWCMS.Core.Helpers;
using BLWCMS.Core.IBusiness;
using BLWCMS.Core.Models;
using Simple.Data;

namespace BLWCMS.Core.Business
{
    public class AdminBusiness:IAdminBusiness
    {
        #region 添加操作
        /// <summary>
        /// 添加操作
        /// </summary>
        /// <param name="adminModel"></param>
        /// <returns></returns>
        public string Add(Models.AdminModel adminModel)
        {
            if (string.IsNullOrEmpty(adminModel.Account) || string.IsNullOrEmpty(adminModel.Password))
            {
                return ResCode.N500("提交信息不完整！");
            }

            adminModel.Status = adminModel.StatusV == "on" ? 1 : 0;
            adminModel.Password = StringHelper.PasswordEncryption(adminModel.Password);

            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                List<AdminModel> adminModels = db.BL_Admins.FindAllByAccount(adminModel.Account).ToList<AdminModel>();
                if (adminModels.Count >= 1)
                {
                    return ResCode.N500("管理员账号已存在！");
                }
                else
                {
                    db.BL_Admins.Insert(adminModel);
                    return ResCode.N200("添加成功！", "/admin");
                }


            }
            catch (Exception e)
            {
                return ResCode.N500(e.Message);
            }
        } 
        #endregion

        #region 编辑操作
        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <param name="adminModel"></param>
        /// <returns></returns>
        public string Edit(Models.AdminModel adminModel)
        {
            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                AdminModel oAdminModel = db.BL_Admins.Get(adminModel.Id);

                adminModel.Password = adminModel.Password == oAdminModel.Password
                    ? adminModel.Password
                    : StringHelper.PasswordEncryption(adminModel.Password);
                adminModel.Status = adminModel.StatusV == "on" ? 1 : 0;

                db.BL_Admins.Update(adminModel);
                return ResCode.N200("编辑成功！", "/admin");
            }
            catch (Exception e)
            {
                return ResCode.N500(e.Message);
            }
        } 
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string Del(int id)
        {
            if (id == 1)
            {
                return ResCode.N500("默认管理员禁止删除！");
            }
            else
            {
                try
                {
                    var db = Database.OpenConnection(Dbcontext.ConnectionString);
                    db.BL_Admins.DeleteById(id);
                    return ResCode.N200("删除成功！", "/admin");
                }
                catch (Exception e)
                {
                    return ResCode.N500(e.Message);
                }
            }
            
        } 
        #endregion

        #region 列表
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.AdminModel> GetList()
        {
            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                return db.BL_Admins.All().OrderByIdDescending().ToList<AdminModel>();
            }
            catch (Exception e)
            {
                return null;
            }
        } 
        #endregion

        #region 单个模型
        /// <summary>
        /// 单个模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Models.AdminModel GetModel(int id)
        {
            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                return db.BL_Admins.Get(id);
            }
            catch (Exception e)
            {
                return null;
            }
        } 
        #endregion
    }
}
